package module.building.view.buildingTips
{
	import code.utils.TimeUtil;
	
	import com.darwin.darwinIsometric.utils.IsoUtils;
	import com.darwin.darwinMVC.mvc.Command;
	import com.darwin.darwinUI.containers.Box;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.general.DarwinFont;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.ToolTipManager;
	import com.darwin.darwinUI.manager.UIManager;
	import com.darwin.darwinUI.manager.base.IToolTip;
	import com.darwin.darwinUI.uiconst.UIAlign;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import main.App;
	
	import module.building.cmds.BuildCommand;
	import module.building.event.BuildEvent;
	import module.building.model.dataObj.BuildingObj;
	import module.main.view.ResourceConst;
	import module.scene.view.iso.BuildingIsoObject;
	
	import org.osmf.events.TimeEvent;
	
	/**
	 * 建造列表中单个建筑项的tips 
	 * @author hc
	 */
	public class BuildingTips extends UIComponent implements IToolTip
	{
		private var _buildingObj:BuildingObj;
		private var _type:int;
		private var _startTime:Date;
		
		private var _buildingName:String;
		private var _durability:String;
		private var _totalDurability:String;
		private var _prosperity:String;
		private var _size:String;
		private var _time:String;
		private var _content:String;
		private var _progress:uint;
		private var _item:*;
		
		private var nameLabel:Label;
		private var baseInfoBox:Box;
		private var contentLabel:Label;
		private var progressLabel:Label;
		private var timeLabel:Label;
		private var contentBg:UIComponent;
		private var tTimer:Timer;
			
		private const TITLE_FONT:DarwinFont = new DarwinFont("",14);
		private const TITLE_COLOR:uint = 0xDFAE64;
		private const NORMAL_FONT:DarwinFont = new DarwinFont("",11);
		private const NORMAL_COLOR:uint = 0xE3DED3;
		
		/**
		 * 构造方法
		 * @param data 包含建筑信息的数据
		 * @param type tips的类型：0是建筑列表tips；1是场景中的建筑通用tips；2是生产中的tips；3是采集点的tips
		 */
		public function BuildingTips()
		{
			addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);
		}
		
		private function removeFromStage(e:Event):void
		{
			if(tTimer)
			{
				tTimer.removeEventListener(TimerEvent.TIMER, countDown);
			}
		}
		
		private function init():void
		{
			mouseEnabled = 
				mouseChildren = false;
			this.backgroundAuto = true;
			this.backgroundSkin = ResourceConst.GENERAL_IMAGE_TIPS_BG;
			
			baseInfoBox = new Box();
			baseInfoBox.autoSize = true;
			baseInfoBox.align = UIAlign.LAYOUT_VERTICAL;
			
			nameLabel = TextFieldFactory.getInstance().getLabel(_buildingObj.objectName);
//			nameLabel = new Label(_buildingName,TITLE_FONT);
//			nameLabel.textColor = TITLE_COLOR;
			
			var time:String = "100";
			var output:String = "";
			var unit:String = "";
			if(_buildingObj.orderArray.length > 0)
			{
				if(_buildingObj.orderArray[0].time < 60)
				{
					time = _buildingObj.orderArray[0].time;
					unit = "分钟";
//				}else if(_data.orderArray[0].time == 30)
//				{
//					time = "半";
//					unit = "小时";
				}else if(_buildingObj.orderArray[0].time >=60)
				{
					time = (_buildingObj.orderArray[0].time/60).toString();
					unit = "小时";
				}
				
				output = parseInt(_buildingObj.orderArray[0].output).toString();
			}
			
//			if(_data.objectName == "building_tips_workshop")
//			{
//				contentLabel = TextFieldFactory.getInstance().getLabel("building_tips_work_description",[1,100]);
//			}else
			contentLabel = TextFieldFactory.getInstance().getLabel(_buildingObj.objectName+"_description",[time,output,unit]);
//			contentLabel = new Label("",NORMAL_FONT);
			contentLabel.autoSize = true;
//			contentLabel.textColor = NORMAL_COLOR;
			contentLabel.backgroundAuto = true;
			contentLabel.backgroundSkin = ResourceConst.GENERAL_IMAGE_TIPS_CONTENT;
			
			switch(_type)
			{
				case 0:
//					addChild(nameLabel);
					addChild(contentLabel);
					baseInfoBox.addItem(TextFieldFactory.getInstance().getLabel("building_tips_prosperity",[_prosperity]));
					baseInfoBox.addItem(TextFieldFactory.getInstance().getLabel("building_tips_size",[_size]));
					baseInfoBox.addItem(TextFieldFactory.getInstance().getLabel("building_tips_buildingTime",[_time]));
					break;
				
				case 1:
					addChild(nameLabel);
					addChild(contentLabel);
					baseInfoBox.addItem(TextFieldFactory.getInstance().getLabel("building_tips_durability",[_durability,_totalDurability]));
//					baseInfoBox.addItem(TextFieldFactory.getInstance().getLabel("building_tips_commercialBuildingOrder_time",[(_item as BuildingIsoObject).absIsoPosition]));
					break;
				
				case 2:
/*					addChild(nameLabel);
					progressLabel = TextFieldFactory.getInstance().getLabel("building_tips_commercialBuildingOrder_timeleft");
					var labelParam:String = TimeUtil.restTimeString(((_item as BuildingIsoObject).dataObj as BuildingObj).currentOrderArr[0].produce_complete_time - App.stime)? TimeUtil.restTimeString(((_item as BuildingIsoObject).dataObj as BuildingObj).currentOrderArr[0].produce_complete_time - App.stime):"??:??:??";
					timeLabel = TextFieldFactory.getInstance().getLabel("building_tips_commercialBuildingOrder_time", [labelParam]);
					timeLabel.x = 58;
					
					progressLabel.addChild(timeLabel);
					if(tTimer)
					{
						tTimer = null;
					}
					tTimer = new Timer(1000);
					tTimer.addEventListener(TimerEvent.TIMER, countDown);
					tTimer.start();
					baseInfoBox.addItem(TextFieldFactory.getInstance().getLabel("building_tips_durability",[_durability,_totalDurability]));
					baseInfoBox.addItem(progressLabel);
					baseInfoBox.addItem(TextFieldFactory.getInstance().getLabel("building_tips_commercialBuildingOrder_output",[output]));*/
					break;
				
				case 3:
					addChild(nameLabel);
					baseInfoBox.addItem(normalLabel("资源种类：秘银矿"));
					baseInfoBox.addItem(normalLabel("当前存储量：98%"));
					baseInfoBox.addItem(normalLabel("容纳部队：7/10"));
					break;
				
				case 4:
					break;
				
				default:break;
			}		
			addChild(baseInfoBox);
		}
		
		/**
		 * 生产倒计时
		 * @param e
		 * 
		 */		
		private function countDown(e:TimerEvent):void
		{
			var leftTime:String = TimeUtil.restTimeString(((_item as BuildingIsoObject).dataObj as BuildingObj).currentOrderArr[0].produce_complete_time - App.stime)? TimeUtil.restTimeString(((_item as BuildingIsoObject).dataObj as BuildingObj).currentOrderArr[0].produce_complete_time - App.stime):"??:??:??";
			trace("leftTime: " + leftTime);
			timeLabel.text = UIManager.getLanguageManager().getText("building_tips_commercialBuildingOrder_time", [leftTime]);;
		}
		
		private function normalLabel(string:String):Label
		{
			var label:Label = new Label(string, NORMAL_FONT);
			label.autoSize = true;
			label.height = 18;
			label.textColor = NORMAL_COLOR;
			
			return(label);
		}
		
		private function initData():void
		{
//			background.width = 185;
//			background.height = 174.6;
			background.width = 150;
			
			width = background.width;
			height = background.height;
			
			baseInfoBox.x = 14.6;
			baseInfoBox.y = 12.8;
			
			background.height = baseInfoBox.y+baseInfoBox.numItems*25;
			
			if(progressLabel)
			{
				progressLabel.width = background.width;
			}
			
			if(this.contains(nameLabel))
			{
				nameLabel.x = (background.width-nameLabel.textWidth)/2;
				nameLabel.y = 11.8;
				baseInfoBox.y = 40.8;//_nameLabel.y + 8;
				background.height += nameLabel.bitmapData.height;
			}
			
			if(this.contains(contentLabel))
			{
//				contentLabel.htmlText = _content;
				contentLabel.x = 13;
				contentLabel.y = baseInfoBox.y+baseInfoBox.numItems*25;
				contentLabel.background.width = background.width-26;
				contentLabel.background.height = contentLabel.bitmapData.height;
				background.height += contentLabel.bitmapData.height + 15.7;
//				contentLabel.background.height = background.height - contentLabel.y - 19;
			}
		}
		
		private function onAdded(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAdded);
		}
		
		private function analyzeData():void
		{
			_buildingName = UIManager.getLanguageManager().getText(_buildingObj.objectName);
			_durability = "100";//_data.durability
			_totalDurability = "100";
			
			_prosperity = ""+_buildingObj.levelInfo[0].prosperity as String;
			var _sizeNum:int = _buildingObj.size as int;
			_size = _sizeNum + " x "+_sizeNum;
//			_content = UIManager.getLanguageManager().getText(_data.description);
			_time = TimeUtil.minutesToData(_buildingObj.levelInfo[0].time).toString();			
		}
		
		public function setData(data:Object,item:*):void
		{
			_item = item;
			_buildingObj = data.data as BuildingObj;
			_type = data.type;
			analyzeData();
			init();
			initData();
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
		}

	}
}